package com.epam.ws.anatomist.messaging.dispatcher;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.handler.HandlerResolver;

import org.apache.log4j.Logger;

import com.epam.ws.anatomist.common.exception.ErrorMessages;
import com.epam.ws.anatomist.common.exception.ServiceInvokerException;

public class ServiceDescriptor
{

	private static final Logger LOGGER = Logger.getLogger(ServiceDescriptor.class);

	private String endpointQNameNamespace;

	private String endpointQNameLocalPart;

	private String portQNameNamespace;

	private String portQNameLocalPart;

	private String wsdlURLLocation;

	private URL wsdlUrl;

	private HandlerResolver m_handleResolver;

	public String getEndpointQNameNamespace()
	{
		return endpointQNameNamespace;
	}

	public void setEndpointQNameNamespace(String endpointQNameNamespace)
	{
		this.endpointQNameNamespace = endpointQNameNamespace;
	}

	public String getEndpointQNameLocalPart()
	{
		return this.endpointQNameLocalPart;
	}

	public void setEndpointQNameLocalPart(String endpointQNameLocalPart)
	{
		this.endpointQNameLocalPart = endpointQNameLocalPart;
	}

	public String getPortQNameNamespace()
	{
		return this.portQNameNamespace;
	}

	public void setPortQNameNamespace(String portQNameNamespace)
	{
		this.portQNameNamespace = portQNameNamespace;
	}

	public String getPortQNameLocalPart()
	{
		return this.portQNameLocalPart;
	}

	public void setPortQNameLocalPart(String portQNameLocalPart)
	{
		this.portQNameLocalPart = portQNameLocalPart;
	}

	public String getWsdlURLocation()
	{
		return this.wsdlURLLocation;
	}

	public void setWsdlURLLocation(String wsdlURLLocation)
	{
		this.wsdlURLLocation = wsdlURLLocation;
		try
		{
			this.wsdlUrl = new URL(wsdlURLLocation);
		}
		catch (MalformedURLException e)
		{
			String errorMessage = String.format(ErrorMessages.MALFORMED_URL_ERROR, wsdlURLLocation);
			LOGGER.error(errorMessage);
			throw new ServiceInvokerException(errorMessage, e);
		}
	}

	public URL getWsdlURL()
	{
		if (this.wsdlUrl == null)
		{
			try
			{
				this.wsdlUrl = new URL(wsdlURLLocation);
			}
			catch (MalformedURLException e)
			{
				String errorMessage = String.format(ErrorMessages.MALFORMED_URL_ERROR, wsdlURLLocation);
				LOGGER.error(errorMessage);
				throw new ServiceInvokerException(errorMessage, e);
			}
		}
		return this.wsdlUrl;
	}

	public QName getPortQname()
	{
		return new QName(portQNameNamespace, portQNameLocalPart);
	}

	public QName getEndpointQname()
	{
		return new QName(endpointQNameNamespace, endpointQNameLocalPart);
	}

	public void setHandleResolver(HandlerResolver handlerResolver)
	{
		this.m_handleResolver = handlerResolver;
	}

	public HandlerResolver getHandleResolver()
	{
		return this.m_handleResolver;
	}

}
